package vip.kassadin.jdbc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import vip.kassadin.entity.User;
import vip.kassadin.jdbc.db.UserRepo;

import java.util.List;

/**
 * Created by kassadin@foxmail.com on 2017/5/1 0001.
 */
@RestController
@RequestMapping
public class UserController {


    @Autowired
    private UserRepo userRepo;

    @RequestMapping("/addUser")
    public User addUser(@RequestParam String username,
                        @RequestParam String password) {
        User user = new User(username, password);
        return userRepo.save(user);
    }

    @RequestMapping("/users")
    public List<User> users() {
        return userRepo.findAll();
    }

    @GetMapping("/user/del")
    public boolean del(@RequestParam  long uid) {
        userRepo.delete(uid);
        return true;
    }

    @GetMapping("/user")
    public User user(@RequestParam  long uid) {
        return userRepo.findOne(uid);
    }
}
